package cn.iocoder.yudao.module.indicator.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * Data: 2020/7/24
 * @author zyw
 */
public class DateUtils {


    private DateUtils() {

    }

    /**
     * 定义日期格式
     */
    public static final String DATE_TYPE_Y_D = "yyyy-MM-dd";
    public static final String DATE_TYPE_Y_S = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TYPE_Y_S2 = "yyyy/MM/dd HH:mm:ss";


    /**
     * @description
     *      按照DATE_TYPE格式来进行转换
     * @param
     * @date 2020/5/27
     * @return java.lang.String
     * @throws
     **/
    public static final String formatDate(Object date) {
        if(null == date) {
            return null;
        } else {
            return formatDate(date, DATE_TYPE_Y_D);
        }
    }

    /**
     * @description
     *      按照指定格式日期来进行转换
     * @param
     * @date 2020/5/27
     * @return java.lang.String
     * @throws
     **/
    public static final String formatDate(Object date, String formatType) {
        if(null == date) {
            return null;
        } else {
            if(StringUtils.isNotEmpty(formatType)) {
                // 说明最终需要根据客户所定义的格式来进行转换
                SimpleDateFormat format = new SimpleDateFormat(formatType);
                return format.format(date);
            } else {
                // 说明你没有给我转你自己的类型(这里千万不要直接return null--->容易抛出格式转换异常的错误)
                return formatDate(date);
            }
        }
    }

    public static Date parseDateString() {
        String dateString = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            d = dt.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * @description
     *      将时间转换为字符串
     * @param
     * @date 2020/5/27
     * @return java.lang.String
     * @throws
     **/
    public static String formatDateAgo(long millisecond) {
        StringBuilder stringBuilder = new StringBuilder();
        if(1000 > millisecond) {
            // 说明只是毫秒
            stringBuilder.append(millisecond).append("毫秒");
        } else {
            // 说明传进来的long类型毫秒数大于1000
            Integer ss = 1000;
            Integer mi = ss * 60;
            Integer hh = mi * 60;
            Integer dd = hh * 24;

            Long day = millisecond / dd;
            Long hour = (millisecond - day * dd) / hh;
            Long minute = (millisecond - day * dd - hour * hh) / mi;
            Long second = (millisecond - day * dd - hour *hh - minute * mi) / ss;

            if(day > 365) {
                return formatDate(new Date(millisecond), "yyyy年MM月dd日 HH时mm分ss秒");
            }
            if(day > 0) {
                stringBuilder.append(day).append("天");
            }
            if(hour > 0) {
                stringBuilder.append(hour).append("小时");
            }
            if(minute > 0) {
                stringBuilder.append(minute).append("分钟");
            }
            if(second > 0) {
                stringBuilder.append(second).append("秒");
            }
        }
        return stringBuilder.toString();
    }

    /**
     * @description
     *      获取系统当前时间
     * @param
     * @date 2020/5/27
     * @return java.lang.String
     * @throws
     **/
    public static final String getCurrentDate() {
        return formatDate(new Date());
    }

    /**
     * @description
     *      获取当年年度
     * @param
     * @date 2020/5/27
     * @return java.lang.Integer
     * @throws
     **/
    public static Integer getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取已经过去的日期 传入-2即前两天
     * @param oldNum formatType 时间格式化字符串
     * @return
     */
    public static String getOldDay(Integer oldNum, String formatType) {
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, oldNum);
        date = calendar.getTime();
        SimpleDateFormat format= new SimpleDateFormat(formatType);
        String dateString = format.format(date);
        return dateString;
    }

    /**
     * 获取已经过去的日期 传入-2即前两天
     * @param oldNum
     * @return
     */
    public static String getOldDay(Integer oldNum) {
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, oldNum);
        date = calendar.getTime();
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(date);
        return dateString;
    }



    /**
     * 获取已经过去的日期 传入-2即前两天
     * @param oldNum
     * @return
     */
    public static String getOldDay(Date date,Integer oldNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, oldNum);
        date = calendar.getTime();
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(date);
        return dateString;
    }

    /**
     * 获取已经过去的月份 传入-1即前一个月
     * @author zyw
     * @param date
     * @param oldNum
     * @return
     */
    public static String getOldMonth(Date date,Integer oldNum){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, oldNum);
        String start = format.format(c.getTime());
        return start;
    }

    /**
     * 获取已经过去的月份 传入-1即前一个月
     * @author zyw
     * @param dateString
     * @param oldMonthNum
     * @return
     */
    public static String getOldMonth(String dateString,Integer oldMonthNum){
        Date date = getDateByDataString(dateString, "yyyy-MM");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, oldMonthNum);
        String start = format.format(c.getTime());
        return start;
    }
    /**
     * 获取已经过去的月份 或年份 +既未来 -既历史
     * @author zyw
     * @param dateString
     * @param oldMonthNum   -1前一个月
     * @param oldYearNum   -1前一年
     * @return
     */
    public static String getOldMonthYear(String dateString,Integer oldMonthNum,Integer oldYearNum){
        Date date = getDateByDataString(dateString, "yyyy-MM");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, oldMonthNum);
        c.add(Calendar.YEAR, oldYearNum);
        String start = format.format(c.getTime());
        return start;
    }

    /**
     * 获取已经过去的年份 传入-1即前一个年
     * @param date
     * @param oldNum
     * @return
     */
    public static String getOldYear(Date date,Integer oldNum){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, oldNum);
        String start =format.format(c.getTime());
        return start;
    }

    /**
     * 获取已经过去的年份 传入-1即前一个年
     * @param dateString
     * @param oldNum
     * @return
     */
    public static String getOldYear(String dateString,Integer oldNum){
        Date date = getDateByDataString(dateString, "yyyy");
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, oldNum);
        String start = format.format(c.getTime());
        return start;
    }
    /**
     * 获取已经过去的年份 传入-1即前一个年
     * @return
     */
    public static Integer getOldQuarter(){
        Calendar c = Calendar.getInstance();
        int m = c.get(Calendar.MONTH) + 1;
        int s = (int)Math.ceil( (m - 1) / 3) + 1;
        return s-1;
    }
    /**
     * 获取已经过去的日期 传入-2即前两天
     * @param oldNum
     * @return
     */
    public static String getOldDayYmd(Date date,Integer oldNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, oldNum);
        date = calendar.getTime();
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(date);
        return dateString;
    }

    /**
     * @Author
     * @Description 时间字符串转成时间
     * @Date 10:09 2021/2/4
     * @Param dateString ：时间字符串 dateType ：时间的格式化类型
     * @return
     */
    public static Date getDateByDataString(String dateString, String dateType) {
        if (StringUtils.isEmpty(dateString)){
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateType);
        try {
            Date date = simpleDateFormat.parse(dateString);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author
     * @Description 时间字符串转换成 毫秒数
     * @Date 10:04 2021/2/4
     * @Param [args]
     * @return void
     */
    public static Long date2Millisecond(Date date) {
        if (null == date) {
            return null;
        }
        return  date.getTime();
    }

    public static String getLastDayOfYear(String year) {
        // 将字符串转换为年份
        int inputYear = Integer.parseInt(year);
        // 创建一个表示该年的第一天的LocalDate对象
        LocalDate firstDayOfYear = LocalDate.of(inputYear, 1, 1);
        // 使用TemporalAdjusters.lastDayOfYear()调整器找到当年的最后一天
        LocalDate lastDayOfYear = firstDayOfYear.with(TemporalAdjusters.lastDayOfYear());
        // 定义日期格式化模式，例如"yyyy-MM-dd"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将最后一天转换为字符串
        String formattedLastDay = lastDayOfYear.format(formatter);
        return formattedLastDay;
    }

    /**
     * 功能描述: 获取月份最后一天
     * @Param:
     * @Return:
     * @Author: ZWC
     * @Date: 2024/3/21
     */
    public static String getLastDayOfMonth(String inputDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            // 解析输入的字符串为LocalDate对象，月份默认为1日
            LocalDate date = LocalDate.parse(inputDate + "-01", formatter);

            // 获取该月的最后一天
            LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());

            // 定义日期格式化模式，例如"yyyy-MM-dd"
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            // 将最后一天转换为字符串并返回
            return lastDayOfMonth.format(outputFormatter);
        } catch (DateTimeParseException e) {
            System.out.println("Invalid date format. Expected 'yyyy-MM'.");
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.getOldMonth("2024-01",-11));
    }
}